using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using StateMachine_Graphical_Editor.Utils;
namespace StateMachine_Graphical_Editor.Forms
{
    /// <summary>
    /// Form raised when need to select transition properties out of a connector which 
    /// represents multiple transitions
    /// </summary>
    public partial class SelectTransitionToEdit : Form
    {
        #region Members:

        private String selectedTransition;

        #region Constants:

        private const String BUTTON_NAME_SUFFIX = "#radioButton";
        private const int Y_GAP = 24;

        #endregion

        #endregion

        #region C'tor:

        public SelectTransitionToEdit()
        {
            InitializeComponent();
        }

        public SelectTransitionToEdit(String transitionName) : this()
        {
            //the first one exists always
            this.radioButton.Name = transitionName;
            this.radioButton.Checked = true;
        }

        /// <summary>
        /// Create a form with radio button list of all transitions presented by a specified connector
        /// </summary>
        /// <param name="transitionNames"></param>
        public SelectTransitionToEdit(MultipleNames transitionNames)
            : this(transitionNames.NamesList[0])
        {
            addRadioButtonsToForm(transitionNames.NamesList);
        }

        #endregion

        #region Properties:

        public String SelectedTransition
        {
            get { return selectedTransition; }
        }

        #endregion

        #region Helpers:

        private RadioButton createRadioButton(
            Point point,
            String transitionName)
        {
            RadioButton button = new RadioButton();
            //properties:
            button.AutoSize = true;
            button.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            //location point
            button.Location = point;
            button.Name = transitionName + BUTTON_NAME_SUFFIX;
            button.Size = new System.Drawing.Size(109, 17);
            button.TabStop = true;
            button.Text = transitionName;
            button.UseVisualStyleBackColor = true;

            return button;
        }

        private void addRadioButtonsToForm(IList<String> names)
        {
            int Xbase = this.radioButton.Location.X;

            //create all other radio buttons
            //start from 1, as the first one is created by C'tor
            for (int i = 1; i < names.Count; i++)
            {
                int YPrevious = this.radioButton.Location.Y;

                RadioButton newButton = createRadioButton(
                    new Point(Xbase, YPrevious + Y_GAP),
                    names[i]);

                this.radioButtonsPanel.Controls.Add(newButton);
            }
        }

        #endregion

        #region Event handlers:

        private void okButton_Click(object sender, EventArgs e)
        {
            //search for the checked button
            foreach (RadioButton button in this.radioButtonsPanel.Controls)
            {
                if (button != null && button.Checked)
                {
                    selectedTransition = button.Text;
                    break;
                }
            }
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }



}